﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public abstract class AlbumPage : System.Web.UI.Page
{
    protected AlbumManager AlbumManager;
    protected PhotoManager PhotoManager;
    protected EmailManager EmailManager;

    protected int AlbumId
    {
        get
        {
            try
            {
                return int.Parse(Server.HtmlEncode(Server.UrlDecode(Request.QueryString[AlbumHelper.AlbumIdKeyString])));
            }
            catch
            {
                return 0;
            }
        }
    }

    protected Album Album
    {
        get
        { return (Album)ViewState["album"]; }
    }

    protected override void OnLoad(EventArgs e)
    {
        AlbumManager = new AlbumManager();
        PhotoManager = new PhotoManager(AlbumId);
        EmailManager = new EmailManager();

        if (!IsPostBack)
        {
            try
            {
                ViewState["album"] = AlbumManager.GetAlbumById(AlbumId);
            }
            catch { }
        }

        base.OnLoad(e);
    }

    protected AlbumViewModel[] GetViewModels()
    {
        AlbumManager = new AlbumManager();
        PhotoManager = new PhotoManager(0);
        Album[] albums = AlbumManager.GetAllAlbums();
        AlbumViewModel[] albumsToDisplay = new AlbumViewModel[albums.Length];
        int albumNumber = 0;
        foreach (Album alb in albums)
        {
            AlbumViewModel albumModel = new AlbumViewModel(alb);
            List<string> imgUrls = PhotoManager.GetUrlsOfAllPhotos(Server.MapPath(alb.FolderPath));
            int i = 0;
            foreach (string url in imgUrls)
            {
                if (i < 5)
                {
                    albumModel.ImagesUrls[i] = ResolveUrl(PhotoManager.GetRelativePath(url));
                    i++;
                }
            }
            while (i < 5)
            {
                albumModel.ImagesUrls[i] = "";
                i++;
            }
            albumsToDisplay[albumNumber] = albumModel;
            albumNumber++;

        }
        return albumsToDisplay;
    }
}